from fastapi import APIRouter, Depends, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession

from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_kylin.entity.vo.future_strategy_param_vo import (
    FutureStrategyParamModel,
    FutureStrategyParamPageQueryModel,
    DeleteFutureStrategyParamModel,
)
from module_kylin.service.future_strategy_param_service import FutureStrategyParamService
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil

# 构建路由
futureStrategyParamController = APIRouter(prefix='/kylin/futureStrategyParam', tags=['期货策略参数管理'],
                                          dependencies=[Depends(LoginService.get_current_user)])


@futureStrategyParamController.get('/list', response_model=PageResponseModel,
                                   dependencies=[
                                       Depends(CheckUserInterfaceAuth('kylin:futureStrategyParam:list'))])
async def get_future_strategy_param_list(
        request: Request,
        param_page_query: FutureStrategyParamPageQueryModel = Depends(
            FutureStrategyParamPageQueryModel.as_query),
        query_db: AsyncSession = Depends(get_db),
        is_page: bool = True
):
    """
    获取策略参数列表
    """
    # 获取分页数据
    param_page_query_result = await FutureStrategyParamService.get_strategy_param_list(
        query_db, param_page_query, is_page
    )
    logger.info('获取期货策略参数列表成功')
    return ResponseUtil.success(model_content=param_page_query_result)


@futureStrategyParamController.get('/{param_id}', response_model=FutureStrategyParamModel,
                                   dependencies=[
                                       Depends(CheckUserInterfaceAuth('kylin:futureStrategyParam:query'))])
async def query_detail_future_strategy_param(request: Request, param_id: int,
                                             query_db: AsyncSession = Depends(get_db)):
    """
    获取策略参数详情
    """
    param_detail_result = await FutureStrategyParamService.get_strategy_param_detail(
        query_db, param_id
    )
    logger.info(f'获取策略参数{param_id}的信息成功')

    return ResponseUtil.success(data=param_detail_result)


@futureStrategyParamController.post('', dependencies=[
    Depends(CheckUserInterfaceAuth('kylin:futureStrategyParam:add'))])
@ValidateFields(validate_model='param')
@Log(title='期货策略参数管理', business_type=BusinessType.INSERT)
async def add_future_strategy_param(
        request: Request,
        param: FutureStrategyParamModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user)
):
    """
    新增策略参数
    """
    add_param_result = await FutureStrategyParamService.add_strategy_param(
        query_db, param
    )
    logger.info(f'用户{current_user.user.user_name}新增期货策略参数成功')

    return ResponseUtil.success(data=add_param_result)


@futureStrategyParamController.put('/{param_id}', dependencies=[
    Depends(CheckUserInterfaceAuth('kylin:futureStrategyParam:edit'))])
@ValidateFields(validate_model='param')
@Log(title='期货策略参数管理', business_type=BusinessType.UPDATE)
async def update_future_strategy_param(
        request: Request,
        param_id: int,
        param: FutureStrategyParamModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user)
):
    """
    更新策略参数
    """
    update_param_result = await FutureStrategyParamService.update_strategy_param(
        query_db, param_id, param
    )
    logger.info(f'用户{current_user.user.user_name}更新期货策略参数{param_id}成功')

    return ResponseUtil.success(data=update_param_result)


@futureStrategyParamController.delete('', dependencies=[
    Depends(CheckUserInterfaceAuth('kylin:futureStrategyParam:delete'))])
@Log(title='期货策略参数管理', business_type=BusinessType.DELETE)
async def delete_future_strategy_param(
        request: Request,
        delete_model: DeleteFutureStrategyParamModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user)
):
    """
    删除策略参数
    """
    delete_param_result = await FutureStrategyParamService.delete_strategy_param(
        query_db, delete_model
    )
    logger.info(f'用户{current_user.user.user_name}删除期货策略参数成功')

    return ResponseUtil.success(msg=delete_param_result.message)

@futureStrategyParamController.post('/truncate', dependencies=[
    Depends(CheckUserInterfaceAuth('kylin:futureStrategyParam:truncate'))])
@Log(title='期货策略参数管理', business_type=BusinessType.DELETE)
async def truncate_future_strategy_param(
        request: Request,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user)
):
    """
    清空全部策略参数
    """
    truncate_param_result = await FutureStrategyParamService.truncate_strategy_param(
        query_db
    )
    logger.info(f'用户{current_user.user.user_name}清空期货策略参数成功')

    return ResponseUtil.success(msg=truncate_param_result.message)
